﻿using System;
using System.Threading.Tasks;
using System.Web.Http;
using Abp.Authorization;
using Abp.Authorization.Users;
using Abp.Web.Models;
using Microsoft.Owin.Infrastructure;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using LeadThing.AbpZeroTemplate.Authorization;
using LeadThing.AbpZeroTemplate.Authorization.Users;
using LeadThing.AbpZeroTemplate.MultiTenancy;
using LeadThing.AbpZeroTemplate.WebApi.Models;
using System.Web;
using LeadThing.AbpZeroTemplate.Files.Dtos;
using System.IO;
using LeadThing.AbpZeroTemplate.Files;
using Abp.Runtime.Session;

namespace LeadThing.AbpZeroTemplate.WebApi.Controllers
{
    public class FileController : AbpZeroTemplateApiControllerBase
    {
        private readonly IFileAppService _fileAppService;

        public FileController(
            IFileAppService fileAppService)
        {
            _fileAppService = fileAppService;
        }


        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public AjaxResponse UploadFile()
        {
            try
            {
                HttpFileCollection files = HttpContext.Current.Request.Files;

                //Check input
                if (files.Count <= 0 || files[0] == null)
                {
                    return new AjaxResponse(L("File_Change_Error"));
                }

                var file = files[0];

                if (file.ContentLength > 20971520) //20MB
                {
                    return new AjaxResponse(L("File_Warn_FileType"));
                }

                //Save new file
                var fileInfo = new FileInfo(file.FileName);
                var tempFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fileInfo.Extension;

                var path = "/Temp/Downloads/" + DateTime.Now.ToString("yyyyMMdd");
                string serverPath = HttpContext.Current.Server.MapPath(path);
                if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))//如果不存在就创建file文件夹
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));

                var returnFilePath = path + "/" + tempFileName;
                var tempFilePath = Path.Combine(serverPath, tempFileName);


                FileEditDto model = new FileEditDto()
                {
                    Id = null,
                    DisID = 0,
                    Title = tempFileName,
                    Extension = fileInfo.Extension,
                    Size = 0,
                    Url = returnFilePath
                };

                var _fileID = _fileAppService.CreateFileAsyncReturnIDAPP(model);

                file.SaveAs(tempFilePath);

                FileModel fileModel = new FileModel() { fileID = _fileID, fileName = tempFileName, fileExtension = fileInfo.Extension, filePath = returnFilePath };

                return new AjaxResponse(fileModel);
            }
            catch (Exception ex)
            {
                return new AjaxResponse(ex.Message);
            }
        }

    }
}
